<template>
  <view class="template-details tn-safe-area-inset-bottom">
    <!-- 顶部自定义导航 -->
    <tn-nav-bar fixed alpha customBack>
      <view slot="back" class='tn-custom-nav-bar__back'
        @click="goBack">
        <text class='icon tn-icon-left'></text>
        <text class='icon tn-icon-home-capsule-fill'></text>
      </view>
    </tn-nav-bar>
    <scroll-view scroll-y="true"
      :scroll-anchoring="true" 
      :refresher-threshold="100"
      :refresher-enabled="true"
      :enable-back-to-top="true"
      :refresher-triggered="triggered"
       @refresherrefresh="refreshList" 
       @refresherrestore="onRestore" 
      scroll-with-animation
      class="classics-scroll" 
      @scrolltolower="getReviews()"
    >
      <view class="" :style="{paddingTop: vuex_custom_bar_height + 'px'}">
        <!-- 图文信息 -->
        <view class="blogger__item">
          <view class="blogger__author tn-flex tn-flex-row-between tn-flex-col-center">
            <view class="justify__author__info" @click="tn('/circlePages/blogger_other')">
              <view class="tn-flex tn-flex-row-center">
                <view class="tn-flex tn-flex-row-center tn-flex-col-center">
                  <view class="">
                    <tn-avatar
                      class=""
                      shape="circle"
                      :src="item.avatar || 'https://fastcdn.hidino.com/hiotk/user.png'"
                      size="lg">
                    </tn-avatar>
                  </view>
                  <view class="tn-padding-right tn-text-ellipsis">
                    <view class="tn-padding-right tn-padding-left-sm tn-text-bold tn-text-lg">{{ item.nickname ? item.nickname : '匿名' }}</view>
                    <view class="tn-padding-right tn-padding-left-sm tn-padding-top-xs tn-color-gray">{{ parseDate(item.createDate || '') }}</view>
                  </view>
                </view>
              </view>
            </view>
            <view class="blogger__author__btn justify-content-item tn-flex-col-center tn-flex-row-center">
              <!-- 既然都点到详情里面了，加个关注按钮呗 -->
              <text class="tn-bg-brown--light tn-round tn-text-df tn-text-bold tn-color-brown" style="padding: 10rpx 24rpx;">+ 关注</text>
            </view>
          </view>
         
          <view class="blogger__desc tn-margin-top-sm tn-margin-bottom-sm tn-text-justify tn-flex-col-center tn-flex-row-left">
            <view v-for="(label_item,label_index) in item.labels" :key="label_index" class="blogger__desc__label tn-float-left tn-margin-right tn-bg-gray--light tn-round tn-text-sm tn-text-bold">
              <text class="blogger__desc__label--prefix">#</text> 
              <text class="tn-text-df">{{ label_item }}</text>
            </view>
            <!-- 不用限制长度了，因为发布的时候限制长度了-->
            <text v-if="!item.labels || item.labels.length < 4" class="blogger__desc__content tn-flex-1 tn-text-justify tn-text-df">{{ item.record }}</text>  
          </view>

          
          <block v-if="item.albums">
            <view v-if="[1,2,4].indexOf(item.albums.length) != -1" class="tn-padding-top-xs">
              <image v-for="(image_item,image_index) in item.albums" :key="image_index" 
                class="blogger__main-image"
                :class="{
                  'blogger__main-image--1 tn-margin-bottom-sm': item.albums.length === 1,
                  'blogger__main-image--2 tn-margin-right-sm tn-margin-bottom-sm': item.albums.length === 2 || item.albums.length === 4
                }"
                :src="image_item"
                mode="aspectFill"
              ></image>
            </view>
            <view v-else class="tn-padding-top-xs">
              <tn-grid  hoverClass="none" :col="3">
                <block v-for="(image_item,image_index) in item.albums" :key="image_index">
                  <tn-grid-item style="width: 30%;margin: 10rpx;">
                    <image
                      class="blogger__main-image blogger__main-image--3"
                      :src="image_item"
                      mode="aspectFill"
                    ></image>
                  </tn-grid-item>
                </block>
              </tn-grid>
            </view>
          </block>
         
          <view class="tn-flex tn-flex-row-between tn-flex-col-center tn-margin-top-xs">
            <view class="justify-content-item tn-flex tn-flex-col-center">
              
              <text class="tn-color-gray">{{ item.viewNum }}人</text>
            </view>
            <view class="justify-content-item tn-color-gray tn-text-center">
              <view class="">
                <text class="blogger__count-icon tn-icon-footprint"></text> 
                <text class="tn-padding-right">{{ item.viewNum }}</text>
                <text class="blogger__count-icon tn-icon-message"></text>
                <text class="tn-padding-right">{{ item.cmtNum }}</text>
                <text class="blogger__count-icon tn-icon-like"></text>
                <text class="">{{ item.likeNum }}</text>
              </view>
            </view>
          </view>
        </view>
        
        
        <!-- 按钮-->
        <view class="tn-flex tn-flex-row-between" style="margin: 40rpx 0 60rpx 0;">
          <view class="tn-flex-1 justify-content-item tn-margin-xs tn-text-center">
            <tn-button v-if="item.ilike" @click="dianzanRecord(item)" backgroundColor="#00FFC6" padding="40rpx 0" width="90%" shadow fontBold>
              <text class="tn-icon-like-fill tn-padding-right-xs tn-color-red"></text>
              <text class="tn-color-black">
                取消点 赞
              </text>
            </tn-button>
            <tn-button v-else @click="dianzanRecord(item)" backgroundColor="#00FFC6" padding="40rpx 0" width="90%" shadow fontBold>
              <text class="tn-icon-like-lack tn-padding-right-xs tn-color-black"></text>
              <text class="tn-color-black">
                点 赞
              </text>
            </tn-button>
          </view>
          <view class="tn-flex-1 justify-content-item tn-margin-xs tn-text-center">
            <tn-button backgroundColor="#FFF00D" padding="40rpx 0" width="90%" shadow fontBold open-type="share">
              <text class="tn-icon-share-triangle tn-padding-right-xs tn-color-black"></text>
              <text class="tn-color-black">分 享</text>
            </tn-button>
          </view>
        </view>
        
        
      </view>
      
      <!-- 评论 -->
      <view class="tn-margin" style="padding-bottom: 120rpx;">
        <!-- 评论2-->
        <!-- 图标logo/头像 -->
        <block v-for="(review, index) in list" :key="index">
          <view class="tn-flex tn-flex-row-between tn-flex-col-center tn-margin-top-xl">
            <view class="justify-content-item">
              <view class="tn-flex tn-flex-col-center tn-flex-row-left">
                <view class="logo-pic tn-shadow" @click="tn('/circlePages/blogger_other')">
                  <view class="logo-image">
                    <tn-avatar
                      class=""
                      shape="circle"
                      size="60rpx"
                      :src="review.userAvatar || 'https://fastcdn.hidino.com/hiotk/user.png'"
                      >
                    </tn-avatar>
                  </view>
                </view>
                <view class="tn-padding-right tn-padding-left-sm">
                  <view class="tn-padding-right tn-text-df tn-text-bold tn-color-black">
                    {{ review.nickname ? review.nickname : '匿名' }}
                  </view>
                  <view class="tn-padding-right tn-text-ellipsis tn-text-xs tn-color-gray" style="padding-top: 5rpx;">
                    {{ parseDate(review.createDate || '') }}
                  </view>
                </view>
              </view>
            </view>
            <view class="justify-content-item tn-flex-row-center tn-flex-col-center tn-color-gray">
              <view class="tn-text-center">
                <text class="tn-icon-like-lack tn-padding-xs"></text>
              </view>
              <view @click="likeReview(review)" class="tn-text-center">
                <text class="tn-text-xs">{{review.likeNum}}</text>
              </view>
            </view>
          </view>
          
          <view 
          @click="replayTo(review)" 
          class="review-content-wrap" 
          v-html="parseToHtml(review.review)"
          style="margin: 20rpx 30rpx 30rpx 80rpx;">
          </view>
          <view v-if="review.children.length" class="tn-bg-gray--light tn-padding-sm" style="margin: 20rpx 30rpx 30rpx 80rpx;border-radius: 10rpx;">
            <block v-for="(child, child_index) in review.children" :key="child_index">
              <text class="tn-text-bold tn-padding-right-xs">{{child.userNickname || '匿名'}}回复{{child.replyUserNickname || '匿名'}}:</text>
              <text class="review-content-wrap"  style="line-height: 40rpx;" v-html="parseToHtml(child.review)"></text>
              <view class="tn-flex tn-flex-row-between tn-margin-top-xs">
                <view class="justify-content-item tn-text-xs tn-color-gray" style="padding-top: 5rpx;">
                  {{ parseDate(child.createDate) }}
                </view>
                <view class="justify-content-item tn-text-xs tn-color-gray">
                  <text class="tn-padding-xs">{{child.likeNum}}</text>
                  <text class="tn-icon-like-lack"></text>
                </view>
              </view>
            </block>
          </view>
        </block>
      </view>
    </scroll-view>
    <view class="footerfixed dd-glass">
      <view class="tn-flex tn-flex-row-between tn-flex-col-center">
        <view class="justify-content-item tn-margin-top tn-margin-bottom">
          <view class="tn-flex tn-flex-row-center tn-flex-col-center">
            <view @click="showEmoji()" 
            class="tn-flex tn-flex-row-center tn-padding-right tn-padding-left">
              <text class="tn-icon-emoji-good tn-text-xxl"></text>
            </view>
            <view class="topic__info__item__input tn-flex tn-flex-direction-row tn-flex-nowrap tn-flex-col-center tn-flex-row-left">
              <view class="topic__info__item__input__content">
                <o-editor
                  @blur="editorBlur"
                  :placeholder="replyTip"
                  ref="oeditor"
                  maxlength="120"
                  placeholder-class="input-placeholder" 
                  @focus="editorFocus" 
                  @onReady="readyHandle" 
                  @input="inputText"
                >
                </o-editor>
              </view>
            </view>
          </view>
        </view>
        <view class="justify-content-item tn-flex-row-center tn-flex-col-center tn-margin-bottom tn-margin-top tn-margin-right">
          <view class="topic__info__item__sure">
            <view class="tn-flex-1 justify-content-item tn-text-center">
              <tn-button @click="submit" :disabled="!inText.text" shape="round" backgroundColor="tn-cool-bg-color-15--reverse" width="100%" shadow>
                <text class="tn-color-white" hover-class="tn-hover" :hover-stay-time="150">
                  发 送
                </text>
              </tn-button>
            </view>
          </view>
        </view>
      </view>
      <view v-show="showEmojiBox" class="emoji-box">
        <o-emoji @click="insertEmoji"></o-emoji>
      </view>
    </view>
  </view>
</template>

<script>
  import { date } from '@/libs/js/utils.js';
  import OInput from '@/components/o-input/o-input.vue';
  import OEditor from '@/components/o-input/o-editor/o-editor.vue';
  import oEmoji  from '@/components/o-input/o-emoji/o-emoji.vue';
  import { parseToText, parseToHtml } from '@/components/o-input/utils/util.js';
  // #ifdef APP-PLUS
  plus.android.importClass("android.view.Window");
  plus.android.importClass("android.view.View");
  plus.android.importClass("android.webkit.WebView");
  // #endif
  import { getRestaurantRecord, postRecordReview,
   getRecordReviews, dianzanRecord } from '@/libs/js/home.js'
  export default {
    components: { oEmoji, OEditor },
    name: 'TemplateDetails',
    data(){
      return {
        item: {
        },
        replyTip: '不说点啥子吗？',
        listQuery: {
          recordId: 0,
          limit: 20,
          page: 1
        },
        showEmojiBox: false,
        inText: {},
        insertEmojiFlag: false,
        posting: false,
        reviewForm: {
          review: ''
        },
        focusing: false,
        triggered: false,
        loading: false,
        loaded: false,
        list: []
      }
    },
    onLoad(options) {
      const id = options.id || 0;
      if(id) {
        this.listQuery.recordId = id;
        this.getInfo();
        this.getReviews();
      }
    },
    created() {
    },
    methods: {
      showEmoji(){
        this.showEmojiBox = !this.showEmojiBox;
      },
      insertEmoji(emoji) {
        this.insertEmojiFlag = true;
        this.editorCtx.insertImage({
          src: emoji.src,
          width: '20px',
          height: '20px',
          extClass: 'emoji',
          data: {
            name: emoji.regName
          }
        })
      },
      parseToHtml,
      submit(){
        if(!this.inText?.text) return;
        let detail = {...this.inText};
        detail.html = parseToHtml(detail.text);
        this.editorCtx.clear();
        this.reviewForm.review = detail.text;
        this.postReview()
        console.log(detail);
      },
      inputText(value) {
        this.inText = parseToText(value);
      },
      editorFocus(e){
      	if(this.insertEmojiFlag){
      		this.insertEmojiFlag = false;
      		return;
      	}
        this.showEmojiBox = false;
      },
      editorBlur(){

      },
      onRestore() {
        this.triggered = false; 
      },
      readyHandle(editor) {
        this.editorCtx = editor;
      },
      blurInput() {
        this.focusing = false;
      }, 
      getInfo() {
        getRestaurantRecord(this.listQuery).then(r => {
          console.log(r);
          if(r.code == 0){
            this.item = {
              ...r.data,
              title: r.data.record,
              albums: r.data.albums && r.data.albums.match(/http/gi) ? r.data.albums.split(',') : '',
              labels: r.data.restaurantsLabels ? JSON.parse(r.data.restaurantsLabels) : [],
            }
          }
        })
      },
      likeReview(review){
        
      },
      dianzanRecord(record){
        dianzanRecord({
          recordId: this.listQuery.recordId,
        }).then(res => {
          this.getInfo()
        })
      },
      parseDate(d) {
        return date('Y-m-d H:i', d)
      },
      replayTo(review) {
        this.reviewForm.replyUserId = review.userId;
        if(review.reviewId){
          this.reviewForm.reviewId = review.reviewId;
        }else{
          this.reviewForm.reviewId = review.id;
        }
        this.focusing = true;
        this.$refs.oeditor.setFocus();
        this.replyTip = '回复' + (review.userNickname || '匿名') + ':'
      },
      refreshList() {
        if(this.loading) return;
        this.loaded = false;
        this.triggered = true;
        
        this.listQuery.page = 1;
        this.getReviews();
        setTimeout(() => {
          this.triggered = false;
        }, 3000)
      },
      getReviews() {
        if(this.loading || this.loaded){
          return;
        }
        this.loading = true;
        getRecordReviews(this.listQuery).then(res => {
          console.log(res)
          this.triggered = false;
          if (res.code === 0 && res.data && res.data.list && res.data.list.length) {
          	const t = res.data.list.map(r => {
          		return {
                ...r,
          			albums: r.albums && r.albums.match(/http/gi) ? r.albums.split(',') : '',
          		}
          	})
            if(this.listQuery.page == 1){
              this.list = t;
            }else{
              this.list = this.list.concat(t);
            }
          
            if(t.length < this.listQuery.limit){
              this.loaded = true;
            }
            console.log(this.list);
          }else{
            this.loaded = true;
          }
          this.listQuery.page++;
          this.loading = false;
          this.$forceUpdate();
        });
      },
      postReview() {
        if(this.posting) return;
        if(this.reviewForm.review){
          this.posting = true;
          postRecordReview({
            restaurantsRecordId: this.listQuery.recordId,
            review: this.reviewForm.review,
            reviewId: this.reviewForm.reviewId,
            replyUserId: this.reviewForm.replyUserId
          }).then(r => {
              console.log(r)
              this.posting = false;
              this.editorCtx.clear();
              this.showEmojiBox = false;
              setTimeout(() => {
                this.getReviews();
              }, 500)
          })
        }
      },
      goBack() {
        uni.navigateBack()
      },
      // 跳转
      tn(e) {
      	uni.navigateTo({
      		url: e,
      	});
      },
    }
  }
</script>

<style lang="scss" scoped>
  /* 胶囊*/
  .emoji-box{
    width: 100%;
    background-color: #efefef;
  }
  .tn-custom-nav-bar__back {
    width: 86px;
    height: 32px;
    margin-left: 15px;
    position: relative;
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    box-sizing: border-box;
    background-color: rgba(0, 0, 0, 0.15);
    border-radius: 1000rpx;
    border: 1rpx solid rgba(255, 255, 255, 0.5);
    color: #FFFFFF;
    font-size: 18px;
    
    .icon {
      display: block;
      flex: 1;
      margin: auto;
      text-align: center;
    }
    
    &:before {
      content: " ";
      width: 1rpx;
      height: 110%;
      position: absolute;
      top: 22.5%;
      left: 0;
      right: 0;
      margin: auto;
      transform: scale(0.5);
      transform-origin: 0 0;
      pointer-events: none;
      box-sizing: border-box;
      opacity: 0.7;
      background-color: #FFFFFF;
    }
  }
  .classics-scroll{
    height: 100vh;
  
    position: sticky;
    top: 0;
    box-sizing: border-box;
  }
  /* 文章内容 start*/
  .blogger {
    &__item {
      padding: 30rpx;
    }
    
    &__author {
      &__btn {
        margin-right: -12rpx;
        padding: 0 20rpx;
      }
    }
    
    &__desc {
      line-height: 55rpx;
      
      &__label {
        padding: 0 20rpx;
        margin: 0rpx 18rpx 0 0;
        
        &--prefix {
          color: #00FFC8;
          padding-right: 10rpx;
        }
      }
      &__content {
        
      }
    }
    
    &__content {
      margin-top: 18rpx;
      padding-right: 18rpx;
      
      &__data {
        line-height: 46rpx;
        text-align: justify;
        overflow: hidden;
        transition: all 0.25s ease-in-out;

      }
      
      &__status {
        margin-top: 10rpx;
        font-size: 26rpx;
        color: #82B2FF;
      }
    }
    
    &__main-image {
      border-radius: 16rpx;
      
      &--1 {
        max-width: 80%;
        max-height: 300rpx;
      }
      
      &--2 {
        max-width: 260rpx;
        max-height: 260rpx;
      }
      
      &--3 {
        height: 212rpx;
        width: 100%;
      }
    }
    
    &__count-icon {
      font-size: 40rpx;
      padding-right: 5rpx;
    }
    
    &__ad {
      width: 100%;
      height: 500rpx;
      transform: translate3d(0px, 0px, 0px) !important;
      
      ::v-deep .uni-swiper-slide-frame {
        transform: translate3d(0px, 0px, 0px) !important;
      }
      .uni-swiper-slide-frame {
        transform: translate3d(0px, 0px, 0px) !important;
      }
      
      &__item {
        position: absolute;
        width: 100%;
        height: 100%;
        transform-origin: left center;
        transform: translate3d(100%, 0px, 0px) scale(1) !important;
        transition: transform 0.25s ease-in-out;
        z-index: 1;
        
        &--0 {
          transform: translate3d(0%, 0px, 0px) scale(1) !important;
          z-index: 4;
        }
        &--1 {
          transform: translate3d(13%, 0px, 0px) scale(0.9) !important;
          z-index: 3;
        }
        &--2 {
          transform: translate3d(26%, 0px, 0px) scale(0.8) !important;
          z-index: 2;
        }
      }
      
      &__content {
        border-radius: 40rpx;
        width: 640rpx;
        height: 500rpx;
        overflow: hidden;
      }
      
      &__image {
        width: 100%;
        height: 100%;
      }
    }
  }
  /* 文章内容 end*/
   
   /* 间隔线 start*/
  .tn-strip-bottom {
   width: 100%;
   border-bottom: 20rpx solid rgba(241, 241, 241, 0.8);
  }
   /* 间隔线 end*/
     
  /* 头像 start */
  .logo-image {
    width: 60rpx;
    height: 60rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
  }
  
  .logo-pic {
    background-size: cover;
    background-repeat: no-repeat;
    // background-attachment:fixed;
    background-position: top;
    box-shadow: 0rpx 0rpx 80rpx 0rpx rgba(0, 0, 0, 0.15);
    border-radius: 50%;
    overflow: hidden;
    // background-color: #FFFFFF;
  }
  
  
  /* 底部 start*/
  .footerfixed{
   position: fixed;
   width: 100%;
   bottom: 0;
   z-index: 999;
   background-color: rgba(255,255,255,0.5);
   box-shadow: 0rpx 0rpx 30rpx 0rpx rgba(0, 0, 0, 0.07);
  }
  
  .tabbar {
    align-items: center;
    min-height: 130rpx;
  	padding: 0;
  	height: calc(130rpx + env(safe-area-inset-bottom) / 2);
  	padding-bottom: calc(30rpx + env(safe-area-inset-bottom) / 2);
    padding-left: 10rpx;
    padding-right: 10rpx;
  }
  
    /* 毛玻璃*/
  .dd-glass {
     width: 100%;
     backdrop-filter: blur(20rpx);
    -webkit-backdrop-filter: blur(20rpx);
  }
  
  /* 头像*/
  .avatar-all {
    width: 60rpx;
    height: 60rpx;
    border: 4rpx solid rgba(255,255,255,0.05);
    border-radius: 50%;
    overflow: hidden;
    box-shadow: 0rpx 0rpx 80rpx 0rpx rgba(0, 0, 0, 0.15);
  }
  .review-content-wrap{
    word-wrap: break-word;
    word-break: break-all;
  }
  /* 内容*/
  .topic {
    position: relative;
    height: 100%;
    z-index: 1;
    margin-bottom: 120rpx;
    
    
    /* 表单信息 start */
    &__info {
      margin: 0 50rpx;
      margin-top: 105rpx;
      padding: 30rpx 51rpx;
      border-radius: 20rpx;
      background-color: rgba(255,255,255,1);
      border: 2rpx solid rgba(255, 255, 255, 0.1);
      box-shadow: 0rpx 10rpx 50rpx 0rpx rgba(0, 3, 72, 0.1);
      
      &__item {
        
        &__input {
          width: 450rpx;
          height: 60rpx;
          border: 1rpx solid #C6D1D8;
          border-radius: 39rpx;
          
          &__left-icon {
            width: 10%;
            font-size: 44rpx;
            margin-left: 20rpx;
            margin-right: 5rpx;
            color: #C6D1D8;
          }
          
          &__content {
            width: 98%;
            padding-left: 10rpx;
            font-size: 30rpx;
            &--verify-code {
              width: 56%;
            }
            
            input {
              font-size: 30rpx;
              color: #78909C;
              // letter-spacing: 0.1em;
            }
          }
          
          &__right-icon {
            width: 10%;
            font-size: 34rpx;
            margin-right: 20rpx;
            color: #78909C;
          }
          
          &__right-verify-code {
            width: 34%;
            margin-right: 20rpx;
          }
        }
        
        &__button {
          width: 100%;
          height: 60rpx;
          text-align: center;
          font-size: 31rpx;
          font-weight: bold;
          line-height: 77rpx;
          // text-indent: 1em;
          border-radius: 100rpx;
          color: #FFFFFF;
          background-color: rgba(255,255,255,0.2);
          // border: 2rpx solid #FFFFFF;
        }
        
        &__sure {
          height: 60rpx;
          width: 140rpx;
        }
        
      }
    }
    /* 表单信息 end */
    
    /* 内容 end */
    
  }
  
  /deep/.input-placeholder {
    font-size: 30rpx;
    color: #C6D1D8;
  }
  
</style>
